package com.drcoffee.drcoffeemall.controller;

import com.drcoffee.drcoffeemall.config.RedisConfig;
import com.drcoffee.drcoffeemall.entry.Attachment;
import com.drcoffee.drcoffeemall.entry.Result;
import com.drcoffee.drcoffeemall.entry.User;
import com.drcoffee.drcoffeemall.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 购物车
 *
 * @author ShiHao Zhou
 */
@RestController
@RequestMapping("/cart")
public class ShoppingCartController {
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ShoppingCartService shoppingCartService;

    /*
     * 根据用户查询购物车
     * */
    @GetMapping("/getCart")
    public Result getCart(@RequestHeader("Token") String Token, @RequestParam String search, @RequestParam String language) {
        return shoppingCartService.getCart(Token, search, language);
    }

    /*
     * 添加至购物车
     * */
    @PostMapping("/addToCart")
    public Result addToCart(@RequestHeader("Token") String Token, @RequestBody Attachment attachment) {
        return shoppingCartService.addToCart(Token, attachment);
    }


    @GetMapping("/removeFromCart")
    public Result removeFromCart(@RequestHeader("Token") String Token, @RequestParam("part_code") List<String> part_code) {
        return shoppingCartService.removeFromCart(Token, part_code);
    }


    /*
     * 修改购物车中数量
     * */
    @PostMapping("/updateQuantityById")
    public Result updateQuantity(@RequestHeader("Token") String Token, @RequestBody Attachment attachment) {
        User user = (User) redisTemplate.opsForValue().get(Token);
        return shoppingCartService.updateQuantityById(user.getAccount(), attachment);
    }

    /*
     * 获取购物车选中项合计金额
     * */
    @PostMapping ("/getTotal")
    public ResponseEntity<Object> getTotal(@RequestHeader("Token") String Token, @RequestBody List<String> part_codes) {
        return shoppingCartService.getTotal(Token, part_codes);
    }

    /*
     * 获取购物车中商品数量
     * */
    @GetMapping("/getCartsCount")
    public Result getCartsCount(@RequestHeader("Token") String Token) {
        return shoppingCartService.getCartsCount(Token);
    }


    /*
     * 获取购物车中商品数量
     * */
    @GetMapping("/getCartProduct")
    public ResponseEntity<Object> getCartProduct(@RequestHeader("Token") String Token, @RequestParam String search, @RequestParam String language) {
        return shoppingCartService.getCartProduct(Token, search, language);
    }

    /**
     * 判断erp料号库存
     */
    @PostMapping("/getErpStock")
    public ResponseEntity<Object> getErpStock(@RequestBody String array){
        return shoppingCartService.getErpStock(array);
    }

}
